package clustersimulator;
import java.util.Random;

class JobGenerator{

	private int minJobs;
	private int maxJobs;
	private int minProcessingTime;
	private int maxProcessingTime;
	private int minADOC;
	private int maxADOC;
	private Random randomGenerator;
	private int jobID;

	public JobGenerator(){
		this(0,8,1,16,1,8,62);
	}

	public JobGenerator(int minJobs, int maxJobs){
		this(minJobs,maxJobs,1,16,1,8,62);
	}

	public JobGenerator(int minJobs, int maxJobs, int minProc, int maxProc){
		this(minJobs,maxJobs,minProc,maxProc,1,8,62);
	}

	public JobGenerator(int minJobs, int maxJobs, int minProc, int maxProc, int minADOC, int maxADOC){
		this(minJobs,maxJobs,minProc,maxProc,minADOC,maxADOC,62);
	}

	public JobGenerator(int minJobs, int maxJobs, int minProc, int maxProc, int minADOC, int maxADOC, long seed){
		this.minJobs = minJobs;
		this.maxJobs = maxJobs;
		this.minProcessingTime = minProc;
		this.maxProcessingTime = maxProc;
		this.minADOC = minADOC;
		this.maxADOC = maxADOC;
		randomGenerator = new Random(seed);
		jobID = 1;
	}

	public Job[] generateJobs(long clock){
		int jobCount = randomFromRange(minJobs,maxJobs,randomGenerator);
		Job[] jobs = new Job[jobCount];

		for (int i=0; i < jobCount; i++){
			int adoc = randomFromRange(minADOC,maxADOC,randomGenerator);
			int pT = randomFromRange(minProcessingTime,maxProcessingTime,randomGenerator); 
			jobs[i] = new Job(jobID,adoc,pT,clock);
			jobID++;
		}

		return jobs;
	}

	private static int randomFromRange(int min, int max, Random generator){
		long range = (long)max - (long)min + 1;
		long fraction = (long)(range * generator.nextDouble());
		return (int)(fraction + min);
	}
}
